package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.exception.HealthException;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service(interfaceClass = MenuService.class)
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    /**
     * 查询所有菜单
     * @return
     */
    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(int roleId) {

        return menuDao.findMenuIdsByRoleId(roleId);
    }

    /*@Override
    public List<Menu> findAllMenuAsTree() {
        // 获取所有菜单
        List<Menu> menuList = menuDao.findAll();
        // 找出所有一级菜单
        List<Menu> menusL1 = menuList.stream().filter(menu -> {
            return menu.getParentMenuId() == null;
        }).map(l1 -> {
            // 封装设置所有一级菜单的子菜单
            List<Menu> children = new ArrayList<>();
            for (Menu menu : menuList) {
                if (menu.getParentMenuId().equals(l1.getId())) {
                    children.add(menu);
                }
            }
            l1.setChildren(children);
            return l1;
        }).collect(Collectors.toList());

        return menusL1;
    }*/

    @Override
    public List<Menu> findAllMenuAsTree() {
        // 获取所有菜单
        List<Menu> menuList = menuDao.findAll();
        // 找出所有一级菜单
        List<Menu> menusL1 = new ArrayList<>();
        for (Menu menu : menuList) {
            if (menu.getParentMenuId() == null) {
                // 封装所有一级菜单的子菜单
                List<Menu> children = new ArrayList<>();
                for (Menu menu1 : menuList) {
                    if (menu1.getParentMenuId() != null && menu1.getParentMenuId().equals(menu.getId())) {
                        children.add(menu1);
                    }
                }
                children.sort((m1, m2) -> m1.getPriority() - m2.getPriority());
                menu.setChildren(children);
                menusL1.add(menu);
            }
        }
        // 排序一级菜单
        menusL1.sort((m1, m2) -> m1.getPriority() - m2.getPriority());

        return menusL1;
    }

    @Transactional
    @Override
    public void add(Menu menu) {
        // 设置path属性
        Integer priority = menu.getPriority();
        Integer parentMenuId = menu.getParentMenuId();
        if (parentMenuId == null) {
            menu.setPath(priority + "");
        } else {
            Menu parentMenu = menuDao.findById(parentMenuId);
            menu.setPath("/" + parentMenu.getPriority() + "-" + priority);
        }
        menuDao.add(menu);
    }

    @Override
    public Menu findById(int id) {
        return menuDao.findById(id);
    }

    @Transactional
    @Override
    public void update(Menu menu) {
        // 设置path属性
        Integer priority = menu.getPriority();
        Integer parentMenuId = menu.getParentMenuId();
        if (parentMenuId == null) {
            menu.setPath(priority + "");
        } else {
            Menu parentMenu = menuDao.findById(parentMenuId);
            menu.setPath("/" + parentMenu.getPriority() + "-" + priority);
        }
        menuDao.update(menu);
    }

    @Transactional
    @Override
    public void deleteById(int id) {
        // 查询当前菜单是否存在子菜单
        int parentMenuCount = menuDao.findByParentMenuId(id);
        if (parentMenuCount > 0) {
            throw new HealthException("请先删除子菜单");
        }

        // 查询当前菜单是否被角色使用
        int roleCount = menuDao.findRoleByMenuId(id);
        if (roleCount > 0) {
            throw new HealthException("该菜单已被角色使用");
        }

        // 删除菜单
        menuDao.deleteById(id);
    }
}
